package com.yuyu.demo.demo_springboot.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuyu.demo.demo_springboot.domain.Girl;
import com.yuyu.demo.demo_springboot.enums.ResultEnum;
import com.yuyu.demo.demo_springboot.exception.GirlException;
import com.yuyu.demo.demo_springboot.repository.GirlRepository;

@Service
public class GirlService {
	@Autowired
	GirlRepository repository;
	
	public Girl findById(Integer id){
		return repository.findOne(id);
	}
	
	public List<Girl> findAll(){
		return repository.findAll();
	}
	
	public Girl save(Girl entity){
		return repository.save(entity);
	}
	
	public void delete(Girl entity){
		repository.delete(entity);
	}
	
	public void getAge(Integer id) throws Exception{
		Girl girl = repository.findOne(id);
		Integer age = girl.getAge();
		if (age<10) {
			throw new GirlException(ResultEnum.PRIMARY_SCHOOL);
		}else if (age>10 && age <16) {
			throw new GirlException(ResultEnum.MIDDLE_SCHOOL);
		}
	}
	
	/**
	 * 用来测试
	 * @param id
	 * @return
	 */
	public Girl findOne(Integer id) {
		return repository.findOne(id);
	}
	
	
}
